/**
 * ! 引用自 ahooks, 如果什么时候有引入这个库, 可以删除此文件, 并修改对应的引用. 版本号 ahooks 2.x, 18c03bd
 */
import { useCallback, useState } from 'react';
import { useUpdateEffect } from 'react-use';

export interface Options<T> {
    defaultValue?: T;
    defaultValuePropName?: string;
    valuePropName?: string;
    trigger?: string;
}

export interface Props {
    [key: string]: any;
}

export default function useControllableValue<T>(props: Props = {}, options: Options<T> = {}) {
    const {
        defaultValue,
        defaultValuePropName = 'defaultValue',
        valuePropName = 'value',
        trigger = 'onChange'
    } = options;

    const value = props[valuePropName] as T;

    const [state, setState] = useState<T | undefined>(() => {
        if (valuePropName in props) {
            return value;
        }
        if (defaultValuePropName in props) {
            return props[defaultValuePropName];
        }
        return defaultValue;
    });

    /* init 的时候不用执行了 */
    useUpdateEffect(() => {
        if (valuePropName in props) {
            setState(value);
        }
    }, [value, valuePropName]);

    const handleSetState = useCallback(
        (v: T | undefined, ...args: any[]) => {
            if (!(valuePropName in props)) {
                setState(v);
            }
            if (props[trigger]) {
                props[trigger](v, ...args);
            }
        },
        [props, valuePropName, trigger],
    );

    return [valuePropName in props ? value : state, handleSetState] as const;
}
